package com;

import java.io.Serializable;

/**
 * @Package: com
 * @ClassName: Singleton
 * @Author: wulongkai
 * @CreateTime: 2024/1/4 19:46
 * @Description:
 */
public class Singleton implements Serializable, Cloneable{

    private volatile static Singleton singleton;

    private Singleton() {
        if (singleton != null) {
            throw new RuntimeException("Don't use this method");
        }
    }

    public static Singleton getSingleton() {
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    // 阻止反序列反射生成对象
    private Object readResolve() {
        return singleton;
    }
}
